// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Ied keypad challenge
// -----------------------------------------------

private [
	"_training",
	"_count",
	"_letter",
	"_lastcount",
	"_lastletter",
	"_time",
	"_object",
	"_param",
	"_letters"
];

_object = _this select 0;
_param  = _this select 3;

if (count _param == 0) then {_training = false} else {_training = true};

_letters = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];

// Arcade = 1
if (wckindofgame == 1) then {
	_count = 0;
} else {
	_count = ceil (wclevel + (random 10));
};

wciedexplosed = false;
wciedcount = 0;
wciedchallenge = true;

wcgarbage = ["Disarm Ied", "Complete the combo keys sequence to disarm ied.", "Press the same numpad keys printed on the screen", 10] spawn WC_fnc_playerhint;
sleep 6;

player playMove "AinvPknlMstpSlayWrflDnon_medic";
wcgarbage = ["GO"] spawn BIS_fnc_dynamicText;
sleep 1;
wcgarbage = [""] spawn BIS_fnc_dynamicText;

_lastletter = "";

while {(wciedcount < _count) and {(!wciedexplosed)}} do {
	_lastcount = wciedcount;
	player playMove "AinvPknlMstpSlayWrflDnon_medic";
	_letter = _letters call BIS_fnc_selectRandom;
	while {_letter == _lastletter} do {
		_letter = _letters call BIS_fnc_selectRandom;
	};
	_lastletter = _letter;
	iedkey = _letter;
	_time = ceil (random 4);
	call compile format ["2 cutRsc ['keypad%1','PLAIN'];", _letter];
	2 cutFadeOut _time;
	sleep _time;
	if (wciedcount - _lastcount != 1) then {wciedexplosed = true};
};

if (wciedexplosed) then {
	wcgarbage = ["FAILED"] spawn BIS_fnc_dynamicText;
	if !(_training) then {
		wchintW = format ["An ied has explosed near %1", name player];
		["wchintW", "client"] call WC_fnc_publicvariable;
		hintSilent wchintW;
		createVehicle ["ARTY_R_227mm_HE", getPosATL player, [], 0, "NONE"];
		createVehicle ["Bo_GBU12_LGB", getPosATL player, [], 0, "NONE"];
		wcteambonusaddscore = -1;
		["wcteambonusaddscore", "server"] call WC_fnc_publicvariable;
	};
} else {
	wcgarbage = ["SUCCESS"] spawn BIS_fnc_dynamicText;
	if !(_training) then {
		wchintW = format ["An ied has been disarm by %1", name player];
		["wchintW", "client"] call WC_fnc_publicvariable;
		hintSilent wchintW;
		wcteambonusaddscore = 3;
		["wcteambonusaddscore", "server"] call WC_fnc_publicvariable;
	};
};

//A3 TODO: setVehicleInit
if !(_training) then {
	_object setVariable ["wciedactivate", false, true];
	_object setVehicleInit "this removeAction 0;";
	processInitCommands;
};

wciedchallenge = false;